	
	/************************************************************************************************
	  
	  Loader(native)
	  
	  1-detects the liquid url
	  3-read the debug option
	  2-loads the jsl module
	  4-executes the script
	  
	*************************************************************************************************/
	
	jsl = function(src) { return eval(src) }
	
	;(function() {
		var childs, a, l, stype, src, url, debug, re, xhr, node, p, script, f, res, req, args, dbv
		
		try {
														
			//@sys: global is not a property of [this] in v8cgi, this.global = undefined
			try 			{ stype = global.v8cgi.version }
			catch(e)	{}
						
			//1° - Web/Desktop system type			
			//-------------------------------------------------------------------------------
			if(!stype) {

				//also file schema to support AdobeAir
				re = /^(http:|https:|file:\/)\/\//i 
				
				//1.1-Liquid url detection
				//-------------------------------------------------------------------------------
				childs 	= document.getElementsByTagName("script")
				l 			= document.location + ""
			
				for(a = 0; a < childs.length; a++) {
					
					//an url can have a query string and/or a fragment id #
					src = childs[a].src.replace(/\s+/, "").split(/\?|#/)[0]
					
					if(src.slice(-9) === "liquid.js") {
						
						//@sys - Explorer v.8.0.6001.18702/Firefox v.3.5.6 &C
						//ie maintains the value passed at <script>.src but other browser normalize it to the absolute url
						url 		= src.replace(/\\/g, "/")
						//document.location is an object not a string
						url 		= re.test(url) ? url : l.slice(0, l.lastIndexOf("/") + 1) + url					
						url	 		= url.slice(0, -9) 					
												
						break
					}
				}
				
				if(!url)	throw Error("liquid url not detected")

				//1.2-debug option 
				//-------------------------------------------------------------------------------
				debug	= childs[a].src.split(/\?|#/)[1]
				dbv		= 2

				if(debug && debug.match(/debug\s*=\s*/)) {
					
					debug = debug.match(/debug\s*=\s*(.)\s*(&|$)/)
					dbv		= debug ? +debug[1] : NaN		
	
					if(isNaN(dbv) || (dbv !== 0 && dbv !== 1 && dbv !== 2))	
																	throw Error("bad value passed to the debug option, expected: [ 0 | 1 | 2 ]")				
				}
				
				//1.3-script loading 
				//-------------------------------------------------------------------------------
				//@sys: Opera v.9.64 - multiple call to this method with same xhr throw an error 
				//			if last open has caused an error : INVALID_STATE_ERR
				//@sys: Explorer 8 - seem to be more affidable the activex than xhr. If we invert the two line the first time
				//			the browser load the html page (only the first time) this script throw an obscure error. I suppose an initialization error.
				//			of xhr. This script is executed twice!!  
				if(window.ActiveXObject)				xhr = new ActiveXObject("MSXML2.XMLHTTP.3.0")
				else if(window.XMLHttpRequest) 	xhr = new XMLHttpRequest()
				else 														throw Error("not supported: XmlHttpRequest/ActiveX(MSXML2.XMLHTTP.3.0)")

				xhr.open("GET", url + "mod/jsl.js", false)

				//@info: no persistent connection				
				xhr.setRequestHeader("Connection", "close")				
				xhr.send("")	//@sys: Netscape/Flock - if null throw an error
				
				if(dbv !== 2)	src = xhr.responseText.replace(/\/\/@d\{[\s\S]+?\/\/@d\}.*\r\n/g, "").replace(/\/\/@d\[[\s\S]+?\/\/@d\].*\r\n/g, "")
				else					src = xhr.responseText
				
				//1.4-script execution
				//-------------------------------------------------------------------------------										
				//@sys: Firefox 3.6.13/ Explorer 8 - returns always a NodeList, never null 
				p = document.getElementsByTagName("head")[0] || document.getElementsByTagName("body")[0]
				if(!p)	throw Error("header/body nodes not found")

				node 			= document.createElement("script")
				node.text = src				

				p.appendChild(node)		
				p.removeChild(node)
			}
			//2° - V8Cgi			
			//-------------------------------------------------------------------------------
			else {
				include("fs", "http")

				//2.1-Liquid url detection
				//-------------------------------------------------------------------------------										
				args	= system.args		
				src 	= args[0]
				
				if(args.length < 2)						throw Error("app not specified")
				if(!(/liquid.js$/).test(src))	throw Error("bad command line arguments, expected: [liquid] [app?liquid-options] [parameters]")

				url = src.replace(/\\/g, "/").replace(/\s+/g, "")
				url = url.slice(0, url.lastIndexOf("/") + 1) + "mod/jsl.js"		
													
				//2.2-debug option 
				//-------------------------------------------------------------------------------
				debug	= args[1].split(/\?|#/)[1]
				dbv		= 2
				
				if(debug && debug.match(/debug\s*=\s*/)) {
					
					debug = debug.match(/debug\s*=\s*(.)\s*(&|$)/)
					dbv		= debug ? +debug[1] : NaN		
	
					if(isNaN(dbv) || (dbv !== 0 && dbv !== 1 && dbv !== 2))	
																	throw Error("bad value passed to the debug option, expected: [ 0 | 1 | 2 ]")				
				}

				//2.3-script loading 
				//-------------------------------------------------------------------------------
				f = new File(url)
				if(!f.isFile()) throw Error("not found (url:" + (system.getcwd() + "/").replace(/\\+/g, "/") + url + ")")
								
				f.open("r")
				script = f.read()
				f.close()										

				//2.4-script execution
				//-------------------------------------------------------------------------------										
				//@info: javascript identifiers: all Unicode chars from categories:
				//first char: Lu, LI, Lt, Lm, Lo, NI. Following chars (first char plus these): Mn, Mc, Nd, Pc
				//a lot of work, simple easy method:
				re = /[\{\}\(\)\[\]\.;,<>=\+\-\*%&\|\^!~\?:\s\d\/\\"'\$\w]/g
				
				var d, d0, d1, d2, d3
						
				//script is a buffer not a string
				if(script != null) {
					try 			{ d0 = script.toString("ISO-8859-1") 	}	catch(e) 	{} 
					try 			{ d1 = script.toString("ASCII") 			}	catch(e) 	{} 
					try 			{ d2 = script.toString("UTF-8") 			}	catch(e) 	{} 
					try 			{	d3 = script.toString("UTF-16") 			}	catch(e) 	{} 	
				}
							
				d = d0 || d1 || d2 || d3
				
				if(d1 && d1.match(re).length > d.match(re).length)	d = d1
				if(d2 && d2.match(re).length > d.match(re).length)	d = d2
				if(d3 && d3.match(re).length > d.match(re).length)	d = d3
																			
				if(dbv !== 2)	
					d = d.replace(/\/\/@d\{[\s\S]+?\/\/@d\}.*\r\n/g, "").replace(/\/\/@d\[[\s\S]+?\/\/@d\].*\r\n/g, "")

				jsl(d)			
			}	
		}
		catch(e) {
			throw Error("liquid(error) >> [Loader] failed to load jsl module - cause: " + e.message)
		}
	})();
